<!DOCTYPE html>
<html>
	<head>
		 <meta charset="UTF-8" />
		<title>replaceDemo</title>
	</head>
	<body>
			<h1 id="content">算法和数据结构是程序的第一秘诀，缺之算法和数据结构是编程的最大原因。</h1>
	</body>
	<script type="text/javascript">
		var str = "算法" + "," + "数据结构";
		//循环的分别赋样式
		for (var i = 0; i < str.split(',').length; i++) {
			var re = "/"+str.split(',')[i]+"/g";
			//获取对象内容
			var txt = document.getElementById("content").innerHTML;
			//给符合要求的文本加上样式替换掉，eval解决转义问题
			document.getElementById("content").innerHTML = txt.replace(eval(re),"<span style='color:red'>"+str.split(',')[i]+"</span>");
		}
	</script>
	 <script type="text/javascript" language="javascript">
     var s = "暖";
     var reg = new RegExp("(" + s + ")", "g");
     var str = "CSDN暖枫无敌，暖枫无敌CSDN";
     var newstr = str.replace(reg, "<font color=red>$1</font>");
     document.write(newstr + "<br />");
 </script>
 <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
</html>